/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

 /***************************************************************************
 * Name: directory_posix.cpp
 *
 * Purpose: directory posix implmentation
 *
 * Developer:
 *   wen.gu , 2019-10-15
 *
 * TODO:
 *
 ***************************************************************************/

 /******************************************************************************
 **    INCLUDES
 ******************************************************************************/



#if defined(HAS_POSIX_DIR_API) && HAS_POSIX_DIR_API
  
#include "directory.h"
#include <dirent.h>
#include <unistd.h>
#include <sys/stat.h>


/******************************************************************************
 **    MACROS
 ******************************************************************************/
#ifndef F_OK
#define F_OK 0
#endif
/******************************************************************************
 **    VARIABLE DEFINITIONS
 ******************************************************************************/



/******************************************************************************
 **    FUNCTION DEFINITIONS
 ******************************************************************************/
class Directory::impl
{
public:
    std::string dir_path_;
public:

};

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

std::shared_ptr<Directory> Directory::OpenDir(const std::string& dir)
{
    if (Directory::IsAccessale(dir))
    {
        std::shared_ptr<Directory> pDir(new Directory());
        pDir->impl_->dir_path_ = dir;
        return pDir;
    }

    return nullptr;
}

//static 
bool Directory::IsAccessale(const std::string& path)
{
    return (access(path.c_str(), F_OK) == 0);
}

//static 
int Directory::MkDir(const std::string& dir)
{
    return mkdir(dir.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
}

//static 
int Directory::RmDir(const std::string& dir)
{
    return rmdir(dir.c_str());
}


Directory::~Directory()
{
    /** todo something */
}


Directory::Directory()
    :impl_(new impl)
{
    /** todo something */
}


/** isDir: true: directory, false: file */
bool Directory::foreach(OnDirLoop on_loop)
{
#if 0    
    DIR *dir = opendir(impl_->dir_path_.c_str());
    if (!dir)
    {
        return false;
    }
#endif

    if (on_loop)
    {
        //struct dirent* entry = readdir(dir);

        struct dirent **namelist;
        int entry_count = scandir(impl_->dir_path_.c_str(), &namelist, 0, alphasort);

        if (entry_count < 0)
        {
            return false;
        }

        int idx = 0;
        struct stat stat_buf;
        std::string file_path;
        while (idx < entry_count)
        {
            struct dirent * entry = namelist[idx];
            file_path = impl_->dir_path_ + entry->d_name;
            stat(file_path.c_str(), &stat_buf);

            //printf("[%s.%d]===>: entry: %s, is_dir: %s, size: %d\n", __FUNCTION__, __LINE__, file_path.c_str(), S_ISDIR(stat_buf.st_mode)? "true":"false", stat_buf.st_size);

            if (on_loop(file_path, S_ISDIR(stat_buf.st_mode), stat_buf.st_size) == false)
            {
                return false;
            }      
            idx++;
        }

        return true;
    }

    return false;
}

#endif /** defined HAVE_POSIX_DIR_API */
